package com.bjpowernode.boss.client;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RedisClient {

    @Resource
    private StringRedisTemplate redisTpl;

    /*一次添加多个值到hash类型*/
    public boolean setHashAll(String key, Map<String, String> value, int timeout, TimeUnit timeUnit) {
        boolean save = true;
        //添加map到hash类型
        redisTpl.opsForHash().putAll(key, value);
        //设置存活时间
        if (timeout > 0) {

            save = redisTpl.expire(key, timeout, timeUnit);

        }
        return save;
    }

    public Object getHashAll(String key, String field) {
        return redisTpl.opsForHash().get(key, field);
    }

    public String getValue(String key) {
        return redisTpl.opsForValue().get(key);
    }

    public void setValue(String key, String value) {
        redisTpl.opsForValue().set(key, value);
    }

    public Boolean exists(String key) {
        return redisTpl.hasKey(key);
    }

    public Boolean deleteHashAll(String key) {
        return redisTpl.delete(key);
    }

    public Boolean deleteKey(String key) {
        return redisTpl.delete(key);
    }

}
